// 项目管理模拟数据
import { ProjectVO, ProjectMemberVO } from '@/api/pypm/project/index'
import { getUserById } from '../system/user'

// 模拟项目数据
const mockProjects: ProjectVO[] = [
  {
    id: '1',
    projectName: '企业官网重构',
    description: '重新设计和开发企业官方网站，提升用户体验和品牌形象',
    startTime: '2023-10-01',
    endTime: '2023-12-31',
    status: '1', // 进行中
    progress: 65,
    priority: '2', // 中等
    managerId: '2',
    manager: getUserById('2'),
    members: [
      {
        id: '1',
        projectId: '1',
        memberId: '2',
        role: '1', // 负责人
        userInfo: getUserById('2')
      },
      {
        id: '2',
        projectId: '1',
        memberId: '3',
        role: '2', // 开发
        userInfo: getUserById('3')
      },
      {
        id: '3',
        projectId: '1',
        memberId: '5',
        role: '3', // 设计
        userInfo: getUserById('5')
      }
    ],
    createBy: 'admin',
    createTime: '2023-09-15 10:00:00',
    updateBy: 'admin',
    updateTime: '2023-11-01 14:30:00',
    remark: '重点项目，需要按期完成'
  },
  {
    id: '2',
    projectName: '用户管理系统开发',
    description: '开发新一代用户管理系统，支持多租户和权限管理',
    startTime: '2023-09-01',
    endTime: '2023-11-15',
    status: '3', // 已完成
    progress: 100,
    priority: '1', // 高
    managerId: '4',
    manager: getUserById('4'),
    members: [
      {
        id: '4',
        projectId: '2',
        memberId: '4',
        role: '1', // 负责人
        userInfo: getUserById('4')
      },
      {
        id: '5',
        projectId: '2',
        memberId: '2',
        role: '2', // 开发
        userInfo: getUserById('2')
      },
      {
        id: '6',
        projectId: '2',
        memberId: '3',
        role: '2', // 开发
        userInfo: getUserById('3')
      }
    ],
    createBy: 'admin',
    createTime: '2023-08-15 10:00:00',
    updateBy: 'admin',
    updateTime: '2023-11-15 16:00:00',
    remark: '已成功交付'
  },
  {
    id: '3',
    projectName: '数据可视化平台规划',
    description: '规划和设计数据可视化平台，整合各系统数据源',
    startTime: '2023-11-10',
    endTime: '2024-02-20',
    status: '1', // 进行中
    progress: 30,
    priority: '2', // 中等
    managerId: '4',
    manager: getUserById('4'),
    members: [
      {
        id: '7',
        projectId: '3',
        memberId: '4',
        role: '1', // 负责人
        userInfo: getUserById('4')
      },
      {
        id: '8',
        projectId: '3',
        memberId: '2',
        role: '2', // 开发
        userInfo: getUserById('2')
      },
      {
        id: '9',
        projectId: '3',
        memberId: '5',
        role: '3', // 设计
        userInfo: getUserById('5')
      }
    ],
    createBy: 'admin',
    createTime: '2023-11-01 10:00:00',
    updateBy: 'admin',
    updateTime: '2023-11-20 10:30:00',
    remark: '处于需求分析阶段'
  },
  {
    id: '4',
    projectName: '移动应用开发',
    description: '开发企业移动应用，支持iOS和Android平台',
    startTime: '2023-12-01',
    endTime: '2024-03-31',
    status: '0', // 未开始
    progress: 0,
    priority: '1', // 高
    managerId: '2',
    manager: getUserById('2'),
    members: [
      {
        id: '10',
        projectId: '4',
        memberId: '2',
        role: '1', // 负责人
        userInfo: getUserById('2')
      },
      {
        id: '11',
        projectId: '4',
        memberId: '3',
        role: '2', // 开发
        userInfo: getUserById('3')
      }
    ],
    createBy: 'admin',
    createTime: '2023-11-15 10:00:00',
    updateBy: 'admin',
    updateTime: '2023-11-15 10:00:00',
    remark: '即将开始'
  },
  {
    id: '5',
    projectName: '系统性能优化',
    description: '优化现有系统性能，提升响应速度和稳定性',
    startTime: '2023-10-15',
    endTime: '2023-11-15',
    status: '2', // 已暂停
    progress: 45,
    priority: '1', // 高
    managerId: '3',
    manager: getUserById('3'),
    members: [
      {
        id: '12',
        projectId: '5',
        memberId: '3',
        role: '1', // 负责人
        userInfo: getUserById('3')
      },
      {
        id: '13',
        projectId: '5',
        memberId: '2',
        role: '2', // 开发
        userInfo: getUserById('2')
      }
    ],
    createBy: 'admin',
    createTime: '2023-10-01 10:00:00',
    updateBy: 'admin',
    updateTime: '2023-11-05 10:30:00',
    remark: '因资源调整暂停'
  }
]

// 获取项目列表
export const getProjectList = () => {
  return mockProjects
}

// 分页获取项目列表
export const getProjectPage = (params: any) => {
  const { pageNum = 1, pageSize = 10, projectName, status, priority } = params
  let filteredProjects = [...mockProjects]

  // 根据项目名称搜索
  if (projectName) {
    filteredProjects = filteredProjects.filter(project => 
      project.projectName.toLowerCase().includes(projectName.toLowerCase())
    )
  }

  // 根据状态筛选
  if (status !== undefined && status !== '') {
    filteredProjects = filteredProjects.filter(project => project.status === status)
  }

  // 根据优先级筛选
  if (priority !== undefined && priority !== '') {
    filteredProjects = filteredProjects.filter(project => project.priority === priority)
  }

  // 分页处理
  const total = filteredProjects.length
  const start = (pageNum - 1) * pageSize
  const end = start + pageSize
  const data = filteredProjects.slice(start, end)

  return {
    total,
    list: data,
    pageNum,
    pageSize
  }
}

// 根据ID获取项目
export const getProjectById = (id: string) => {
  return mockProjects.find(project => project.id === id)
}

// 创建项目
export const createProject = (projectData: Omit<ProjectVO, 'id' | 'createTime' | 'updateTime'>) => {
  const newProject: ProjectVO = {
    ...projectData,
    id: `${Date.now()}`,
    createTime: new Date().toLocaleString('zh-CN'),
    updateTime: new Date().toLocaleString('zh-CN')
  }
  mockProjects.unshift(newProject)
  return newProject
}

// 更新项目
export const updateProject = (id: string, projectData: Partial<ProjectVO>) => {
  const index = mockProjects.findIndex(project => project.id === id)
  if (index !== -1) {
    mockProjects[index] = {
      ...mockProjects[index],
      ...projectData,
      updateTime: new Date().toLocaleString('zh-CN')
    }
    return mockProjects[index]
  }
  return null
}

// 删除项目
export const deleteProject = (id: string) => {
  const index = mockProjects.findIndex(project => project.id === id)
  if (index !== -1) {
    mockProjects.splice(index, 1)
    return true
  }
  return false
}

// 获取项目统计数据
export const getProjectStatistics = () => {
  const total = mockProjects.length
  const inProgress = mockProjects.filter(p => p.status === '1').length
  const completed = mockProjects.filter(p => p.status === '3').length
  const notStarted = mockProjects.filter(p => p.status === '0').length
  const suspended = mockProjects.filter(p => p.status === '2').length

  return {
    total,
    inProgress,
    completed,
    notStarted,
    suspended
  }
}